<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/26
 * Time: 15:43
 */

namespace app\http\controller;


use app\admin\controller\BaseController;
use app\admin\model\OrderFoods;
use app\admin\model\ShopTable;
use app\index\model\Member;
use app\index\model\Order;
use think\Controller;

class PushController extends Controller
{
    //推送订单号
    public function pushStr($shop_id){
        $order = Order::where('push_status',0)->where('table_id','<>',0)
            ->where('shop_id',$shop_id)->field('id,order_no')->find();
        if (!$order){
            return 0;
        }
        Order::update(['push_status'=>1],['id'=>$order['id']]);
        return $order['order_no'];
    }

    public function foodConfirm(){
        $food_id = input('post.food_id');
        $cook_id = input('post.cook_id');
        $ret = OrderFoods::where('id',$food_id)->find();
        if (!$ret){
            return json(['code'=>0,'msg'=>'菜品不存在']);
        }elseif ($ret['cook_id']){
            return json(['code'=>0,'msg'=>'厨师已绑定']);
        }
        $cook = OrderFoods::update(['cook_id'=>$cook_id],['id'=>$food_id]);
        if (!$cook){
            return json(['code'=>0,'msg'=>'绑定失败']);
        }
        $base = new BaseController;
        $data = $base->cook($food_id);
        return json(['code'=>1,'msg'=>'绑定成功']);
    }

    //订单数据
    public function pushData(){
        $shop_id = session('admin');
        $order_no = input('post.order_no');
        $order = Order::with(['orderFoods'=>function($query){
            $query->field('order_no,food_name,spec,quantity');
        }])->where('order_no',$order_no)->find();
        if (!$order){
            return json(['code'=>0,'msg'=>'无数据']);
        }
        $cook = Member::where('status',3)
            ->where('staff_status',1)->where('shop_id',$shop_id)->field('id,true_name')->select()->toArray();
        $data = [];
        $table = ShopTable::where('id',$order['table_id'])->field('id,table_no')->find();
        foreach ($order['order_foods'] as $k=>$v){
            $data[$k]['food'] = $v;
            $data[$k]['table'] = $table['table_no'];
            $data[$k]['content'] = $order['content'];
            $data[$k]['create_time'] = $order['order_time'];
        }
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data,'cook'=>$cook]);
    }

    public function pushAll(){
        $shop_id = session('admin');
        $status  = (int)input('post.status')?[['cook_id','<>',0]]:[['cook_id','=',0]];
        $order = Order::with(['orderFoods'=>function($query)use($status){
            $query->where($status)->field('order_no,food_name,spec,quantity,cook_id');
        }])->where($shop_id)->where('table_id','<>',0)->select()->toArray();
        $cook = Member::where('status',3)
            ->where('staff_status',1)->where('shop_id',$shop_id)->field('id,true_name')->select()->toArray();
        if ((int)input('post.status')){
            $data = $this->pushOn($order);
        }else{
            $data= $this->pushNot($order);
        }
        if (!$data){
            return json(['code'=>0,'msg'=>'无数据']);
        }
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data,'cook'=>$cook]);
    }

    private function pushNot($order){
        $data = [];
        foreach ($order as $item){
            $table = ShopTable::where('id',$item['table_id'])->field('id,table_no')->find();
            foreach ($item['order_foods'] as $k=>$v){
                $data[$k]['food'] = $v;
                $data[$k]['table'] = $table['table_no'];
                $data[$k]['content'] = $item['content'];
                $data[$k]['create_time'] = $item['order_time'];
            }
        }
        return $data;
    }

    private function pushOn($order){
        $data = [];
        foreach ($order as $item){
            $table = ShopTable::where('id',$item['table_id'])->field('id,table_no')->find();
            foreach ($item['order_foods'] as $k=>$v){
                $data[$k]['food'] = $v;
                $cook = Member::where('id',$v['cook_id'])->field('id,true_name')->find();
                $data[$k]['cook'] = $cook['true_name'];
                $data[$k]['table'] = $table['table_no'];
                $data[$k]['content'] = $item['content'];
                $data[$k]['create_time'] = $item['order_time'];
            }
        }
        return $data;
    }


}